import Cookies from "js-cookie";
// 访问 token 缓存的 key
const ACCESS_TOKEN_KEY = "access_token";
// 访问 appToken 缓存的 key
const ACCESS_TOKEN_APP_KEY = "access_app_token";
// 访问 是否拥有操作权限 缓存的 key
const HAS_OPERATE_PERMISSION = "has_operate_permission";

function getToken(): string {
  return Cookies.get(ACCESS_TOKEN_KEY) || "";
}

function setToken(token: string) {
  Cookies.set(ACCESS_TOKEN_KEY, token);
}

function clearToken() {
  Cookies.remove(ACCESS_TOKEN_KEY);
}

function getAppToken(): string {
  return Cookies.get(ACCESS_TOKEN_APP_KEY) || "";
}

function setAppToken(token: string) {
  Cookies.set(ACCESS_TOKEN_APP_KEY, token);
}

function clearAppToken() {
  Cookies.remove(ACCESS_TOKEN_APP_KEY);
}
function getHasOperatePermission(): boolean {
  const hasOperatePermission: string | undefined = Cookies.get(HAS_OPERATE_PERMISSION);
  return !!(hasOperatePermission && [1, 2].includes(Number(hasOperatePermission)));
}

function setHasOperatePermission(operatePermission: number) {
  Cookies.set(HAS_OPERATE_PERMISSION, String(operatePermission));
}

function clearHasOperatePermission() {
  Cookies.remove(HAS_OPERATE_PERMISSION);
}

export {
  getToken,
  setToken,
  clearToken,
  getAppToken,
  setAppToken,
  clearAppToken,
  getHasOperatePermission,
  setHasOperatePermission,
  clearHasOperatePermission,
};
